﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMARTMobile.Common;
using SmartMobile.Data;
using SMARTMobile.Models;
using SMARTMobile.WinPhone;

namespace SMARTMobile.ViewModels
{
    public class PriceRangeContributionViewModel : ViewModelBase
    {
        #region Fields

        private SmartMobileDBContext _smartMobileContext;
        private ObservableCollection<PriceRangeContributionExtend> _priceRangeContributionObserable;

        #endregion

        public PriceRangeContributionViewModel()
        {
            this.CreateConnection();
            this.GetPriceRangeContributionn();

            this.SaveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteSaveCommand
            };

            this.ExitCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitCommand
            };
        }

        #region Properties

        public ObservableCollection<PriceRangeContributionExtend> PriceRangeContributionObserable
        {
            get { return this._priceRangeContributionObserable; }
            set
            {
                this._priceRangeContributionObserable = value;
                base.RaisePropertyChanged("PriceRangeContributionObserable");
            }
        }

        #endregion

        #region Command

        public ICommandRoute ExitCommand
        {
            get;

            private set;
        }

        public ICommandRoute SaveCommand
        {
            get;

            private set;
        }

        #endregion

        #region Command Action Handlers

        private void ExecuteExitCommand(object commandParams)
        {
            this.ExitCommand.RaiseCanExecuteChanged(true);
            base.PageNavigation.Navigate("InputData");
        }

        private void ExecuteSaveCommand(object commandParams)
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                this._smartMobileContext.PriceRangeContribution.DeleteAllOnSubmit(
                    this._smartMobileContext.PriceRangeContribution.Where(
                        x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());

                this._smartMobileContext.SubmitChanges();
            }

            List<PriceRangeContribution> PriceRangeContributionList = new List<PriceRangeContribution>();
            double? value = 0;
            double? volume = 0;
            foreach (var obj in this.PriceRangeContributionObserable)
            {
                PriceRangeContribution vender = new PriceRangeContribution
                {
                    ID_PK = obj.ID_PK,
                    CustomerKey = obj.CustomerKey,
                    ContributionByValue = obj.ContributionByValue,
                    ContributionByVolume = obj.ContributionByVolume,
                    PriceRangeCode = obj.PriceRangeCode
                };
                value += obj.ContributionByValue;
                volume += obj.ContributionByVolume;

                PriceRangeContributionList.Add(vender);
            }

            if (value != 100 || volume != 100)
            {
                this.ShowMessage("Tỉ lệ chưa đúng 100%");
                this.SaveCommand.RaiseCanExecuteChanged(true);
                return;
            }

            this._smartMobileContext.PriceRangeContribution.InsertAllOnSubmit(PriceRangeContributionList);
            this._smartMobileContext.SubmitChanges();

            this.IsBusy = false;

            this.ShowMessage("Lưu thành công");
            this.SaveCommand.RaiseCanExecuteChanged(true);
        }

        #endregion

        #region Method

        private void GetPriceRangeContributionn()
        {
            this.PriceRangeContributionObserable = new ObservableCollection<PriceRangeContributionExtend>();
            // Tao danh sach mac dinh
            List<PriceRange> priceRanges = this._smartMobileContext.PriceRange.ToList();
            if (priceRanges.Count == 0)
            {
                return;
            }

            foreach (var obj in priceRanges)
            {
                PriceRangeContributionExtend priceRangeContribution = new PriceRangeContributionExtend
                {
                    ID_PK = Guid.NewGuid(),
                    CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    ContributionByValue = 0,
                    ContributionByVolume = 0,
                    PriceRangeCode = obj.PriceRangeCode,
                    PriceRangeName = obj.Descriptionn
                };

                this.PriceRangeContributionObserable.Add(priceRangeContribution);
            }

            if (GlobalInformation.SelectedTCARCUS != null)
            {
                List<PriceRangeContribution> priceRangeContributions = this._smartMobileContext.PriceRangeContribution.Where(p => p.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList();
                if (priceRangeContributions.Count > 0)
                {
                    // Khi co du du lieu
                    if (priceRangeContributions.Count == this.PriceRangeContributionObserable.Count)
                    {
                        this.PriceRangeContributionObserable = new ObservableCollection<PriceRangeContributionExtend>();
                        foreach (var obj in priceRangeContributions)
                        {
                            PriceRangeContributionExtend priceRangeContribution = new PriceRangeContributionExtend
                            {
                                ID_PK = obj.ID_PK,
                                CustomerKey = obj.CustomerKey,
                                ContributionByValue = obj.ContributionByValue,
                                ContributionByVolume = obj.ContributionByVolume,
                                PriceRangeCode = obj.PriceRangeCode,
                                PriceRangeName = this.GetPriceRangeName(obj.PriceRangeCode)
                            };

                            this.PriceRangeContributionObserable.Add(priceRangeContribution);
                        }
                    }
                    else
                    {
                        // Khi thieu du lieu
                        foreach (var obj in priceRangeContributions)
                        {
                            PriceRangeContributionExtend selectedItem = this.PriceRangeContributionObserable.FirstOrDefault(p => (p.CustomerKey == obj.CustomerKey && p.PriceRangeCode == obj.PriceRangeCode));
                           
                            if (selectedItem != null)
                            {
                                selectedItem.ID_PK = obj.ID_PK;
                                selectedItem.CustomerKey = obj.CustomerKey;
                                selectedItem.ContributionByValue = obj.ContributionByValue;
                                selectedItem.ContributionByVolume = obj.ContributionByVolume;
                                selectedItem.PriceRangeCode = obj.PriceRangeCode;
                                selectedItem.PriceRangeName = this.GetPriceRangeName(obj.PriceRangeCode);
                            }   
                        }

                        this._smartMobileContext.PriceRangeContribution.DeleteAllOnSubmit(this._smartMobileContext.PriceRangeContribution);
                        this._smartMobileContext.SubmitChanges();

                        // Cap nhat du lieu lai 
                        List<PriceRangeContribution> PriceRangeContributionList = new List<PriceRangeContribution>();                      
                        foreach (var obj in this.PriceRangeContributionObserable)
                        {
                            PriceRangeContribution priceRange = new PriceRangeContribution
                            {
                                ID_PK = obj.ID_PK,
                                CustomerKey = obj.CustomerKey,
                                ContributionByValue = obj.ContributionByValue,
                                ContributionByVolume = obj.ContributionByVolume,
                                PriceRangeCode = obj.PriceRangeCode
                            };

                            PriceRangeContributionList.Add(priceRange);
                        }

                        this._smartMobileContext.PriceRangeContribution.InsertAllOnSubmit(PriceRangeContributionList);
                        this._smartMobileContext.SubmitChanges();
                    }
                }
                else
                {
                    // Cap nhat du lieu lai 
                    List<PriceRangeContribution> PriceRangeContributionList = new List<PriceRangeContribution>();
                    foreach (var obj in this.PriceRangeContributionObserable)
                    {
                        PriceRangeContribution priceRange = new PriceRangeContribution
                        {
                            ID_PK = obj.ID_PK,
                            CustomerKey = obj.CustomerKey,
                            ContributionByValue = obj.ContributionByValue,
                            ContributionByVolume = obj.ContributionByVolume,
                            PriceRangeCode = obj.PriceRangeCode
                        };

                        PriceRangeContributionList.Add(priceRange);
                    }

                    this._smartMobileContext.PriceRangeContribution.InsertAllOnSubmit(PriceRangeContributionList);
                    this._smartMobileContext.SubmitChanges();
                }
            }            
        }

        private string GetPriceRangeName(string priceRangeCode)
        {
            return this._smartMobileContext.PriceRange.Where(p => p.PriceRangeCode == priceRangeCode).Select(p => p.Descriptionn).FirstOrDefault(); 
        }

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }

        #endregion
    }
}